package com.zucc.config;

import com.zucc.exception.SyzxException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadBase;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Resp bizExceptionHandler(Exception e){
        log.error("执行出现异常:{}",e.getMessage(), e);
        int code = -1;
        String msg = "系统错误";
        if (e instanceof SyzxException){
            code =((SyzxException) e).getCode();
            msg  = ((SyzxException) e).getMsg();
        }
        return buildResponse(code,msg);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Resp messageNotReadableExceptionHandler(Exception e) {
        log.error("参数不合法",e);
        return buildResponse(-1,"参数不合法，content-type是application/json，请确认请求方式");
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Resp missingServletRequestParameterExceptionHandler(Exception e) {
        log.error("参数不合法",e);
        MissingServletRequestParameterException exception = (MissingServletRequestParameterException) e;
        String msg = "the type of " +exception.getParameterName() + " should be " + exception.getParameterType();
        return buildResponse(-1,msg);
    }



    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public Resp bindingExceptionHandler(Exception e) {
        log.error("参数不合法",e);
        BindingResult bindingResult = null;
        if (e instanceof BindException) {
            bindingResult = ((BindException) e).getBindingResult();
        } else if (e instanceof MethodArgumentNotValidException) {
            bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
        }
        return buildResponse(-1,bindingResult.getFieldError().getDefaultMessage());
    }

    @ExceptionHandler({MethodArgumentTypeMismatchException.class})
    public Resp typeDismatch(MethodArgumentTypeMismatchException exception) {
        log.error("参数不合法",exception);
        String name = exception.getName();
        return buildResponse(-1,name + "参数转换异常");
    }

    @ExceptionHandler({FileUploadBase.FileSizeLimitExceededException.class})
    public Resp fileMaxSize(MethodArgumentTypeMismatchException exception) {
        log.error("文件超过最大限制",exception);
        String name = exception.getName();
        return buildResponse(-1,"上传文件超过最大限制");
    }


    @ExceptionHandler({MaxUploadSizeExceededException.class})
    public Resp fileMaxSize2(MethodArgumentTypeMismatchException exception) {
        log.error("文件超过最大限制",exception);
        String name = exception.getName();
        return buildResponse(-1,"上传文件超过最大限制");
    }

    private Resp buildResponse(int code, String msg) {
        Resp response = new Resp();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }
}
